﻿using CBS.APP;
using CBS.DB.CBSEntity;
using CBS.Model.UIModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// AddStudent.xaml 的交互逻辑
    /// </summary>
    public partial class AddStudent : Window
    {
        public AddStudent()
        {
            InitializeComponent();

            BindSemester();
            BindIntroduceStudent();
        }

        private async void BindSemester()
        {
            try
            {
                var list = await Task.Run(() => SemesterApp.GetSelectList());

                if (list.Count == 0) return;

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Semester.ItemsSource = list;
                }));
                    cb_Semester.SelectedIndex = 0;
                    //BindClassPlan();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindIntroduceStudent()
        {
            try
            {
                var list = await Task.Run(() => StudentApp.GetSelectList());
                list.Insert(0, new StudentSelectModel { StudentId = 0, StudentName = "无" });

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Introduce.ItemsSource = list;
                    cb_Introduce.SelectedIndex = 0;
                }));
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int semesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
            int intrduceStudentId = ((StudentSelectModel)cb_Introduce.SelectedItem).StudentId;

            string studentName = tb_StudentName.Text.Trim();
            string phoneNumber = tb_PhoneNumber.Text.Trim();
            string wxCode = tb_WxCode.Text.Trim();
            string accountStr = tb_Account.Text.Trim();
            bool parseResult = decimal.TryParse(accountStr, out decimal account);
            if (!string.IsNullOrWhiteSpace(accountStr) && !parseResult)
            {
                MessageBox.Show(this, "账户余额填写格式错误！", "填写错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            StudentViewModel studentViewModel = new StudentViewModel()
            {
                StudentName = studentName,
                Balance = account,
                PhoneNumber = phoneNumber,
                WxCode = wxCode,
            };

            string msg = string.Empty;
            if (StudentApp.CreateStudent(studentViewModel, out msg))
            {
                this.DialogResult = true;
            }
            else
            {
                MessageBox.Show(this, msg, "操作失败", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
        }

        private void tb_StudentName_LostFocus(object sender, RoutedEventArgs e)
        {
            string studentName = tb_StudentName.Text.Trim();
            if (string.IsNullOrWhiteSpace(studentName)) 
            {
                tb_StudentName.Background = Brushes.White;
                return;
            } 

            try
            {
                if (StudentSignUpApp.CheckStudentName(studentName))
                {
                    tb_StudentName.Background = Brushes.Yellow;
                }
                else
                {
                    tb_StudentName.Background = Brushes.White;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
